package gateway.internal.config.impl;

import gateway.config.Config;
import gateway.config.ConnectorConfig;
import gateway.config.FilterConfig;
import gateway.config.InboundConfig;
import gateway.config.OutboundConfig;
import gateway.config.RouteConfig;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Jin Zheng
 * @since 1.0 2020-09-15
 */
public class DefaultConfig implements Config
{
	private Map<String, ConnectorConfig> connectorConfigMap = new HashMap<>();
	private Map<String, InboundConfig> inboundConfigMap = new HashMap<>();
	private Map<String, OutboundConfig> outboundConfigMap = new HashMap<>();
	private Map<String, FilterConfig> filterConfigMap = new HashMap<>();
	private Map<String, RouteConfig> routeConfigMap = new HashMap<>();

	public DefaultConfig()
	{
	}

	@Override
	public ConnectorConfig getConnectorConfig(String id)
	{
		return connectorConfigMap.get(id);
	}

	@Override
	public void putConnectorConfig(ConnectorConfig connectorConfig)
	{
		if (connectorConfigMap.containsKey(connectorConfig.id()))
			throw new IllegalArgumentException("Connector config exist, id: " + connectorConfig.id());

		connectorConfigMap.put(connectorConfig.id(), connectorConfig);
	}

	@Override
	public List<ConnectorConfig> getConnectorConfigs()
	{
		return List.copyOf(connectorConfigMap.values());
	}

	@Override
	public InboundConfig getInboundConfig(String id)
	{
		return inboundConfigMap.get(id);
	}

	@Override
	public void putInboundConfig(InboundConfig inboundConfig)
	{
		if (inboundConfigMap.containsKey(inboundConfig.id()))
			throw new IllegalArgumentException("Inbound config exist, id: " + inboundConfig.id());

		inboundConfigMap.put(inboundConfig.id(), inboundConfig);
	}

	@Override
	public List<InboundConfig> getInboundConfigs()
	{
		return List.copyOf(inboundConfigMap.values());
	}

	@Override
	public OutboundConfig getOutboundConfig(String id)
	{
		return outboundConfigMap.get(id);
	}

	@Override
	public void putOutboundConfig(OutboundConfig outboundConfig)
	{
		if (outboundConfigMap.containsKey(outboundConfig.id()))
			throw new IllegalArgumentException("Outbound config exist, id: " + outboundConfig.id());

		outboundConfigMap.put(outboundConfig.id(), outboundConfig);
	}

	@Override
	public List<OutboundConfig> getOutboundConfigs()
	{
		return List.copyOf(outboundConfigMap.values());
	}

	@Override
	public FilterConfig getFilterConfig(String id)
	{
		return filterConfigMap.get(id);
	}

	@Override
	public void putFilterConfig(FilterConfig filterConfig)
	{
		if (filterConfigMap.containsKey(filterConfig.id()))
			throw new IllegalArgumentException("Filter config exist, id: " + filterConfig.id());

		filterConfigMap.put(filterConfig.id(), filterConfig);
	}

	@Override
	public List<FilterConfig> getFilterConfigs()
	{
		return List.copyOf(filterConfigMap.values());
	}

	@Override
	public RouteConfig getRouteConfig(String id)
	{
		return routeConfigMap.get(id);
	}

	@Override
	public void putRouteConfig(RouteConfig routeConfig)
	{
		if (routeConfigMap.containsKey(routeConfig.id()))
			throw new IllegalArgumentException("Route config exist, id: " + routeConfig.id());

		routeConfigMap.put(routeConfig.id(), routeConfig);
	}

	@Override
	public List<RouteConfig> getRouteConfigs()
	{
		return List.copyOf(routeConfigMap.values());
	}

}
